<?php
// +----------------------------------------------------------------------
// | snake
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://baiyf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai <1902822973@qq.com>
// +----------------------------------------------------------------------
namespace app\common\model;

use think\Model;

class Activity extends Model
{
    protected $dateFormat = 'Y-m-d H:i:s';
    protected $type = [
        'start_time'  =>  'timestamp',
        'end_time'  =>  'timestamp',
    ];

    public static $status = [
        0 => '关闭',
        1 => '开启'
    ];

    public function allActivity($map, $offset, $limit)
    {
        return $this->where($map)->limit($offset,$limit)->select();
    }

    public function add($data)
    {
        $data['create_time'] = time();
        return $this->insert($data);
    }

    public function edit($data)
    {
        return $this->update($data);
    }

    // 查询进行中的活动
    public static function underwayActivity($map = [], $is_one = true)
    {
        $map['status'] = 1;
        $map['end_time'] = ['gt', time()];
        if($is_one) {
            return Activity::where($map)->order('sort asc')->find();
        } else {
            return Activity::where($map)->order('sort asc')->select();
        }
    }

    // 关联赞助商
    public function sponsor()
    {
        return $this->hasMany('ActivitySponsor', 'aid');
    }

    // 关联奖品
    public function prize()
    {
        return $this->hasMany('ActivityPrize', 'aid');
    }
}
